package com.leoman.work.dao;

import com.leoman.common.dao.IBaseJpaRepository;
import com.leoman.work.entity.WorkData;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * workDataDao
 * Created by 涂奕恒 on 2016/12/14.
 */
public interface WorkDataDao extends IBaseJpaRepository<WorkData> {

    @Query("select a from WorkData a where a.work.id = ?1 and a.detailId = ?2 and a.createDate = ?3")
    WorkData findOneByDetailIdAndCreateDate(Long workId, Long detailId, String createDate);

    @Query("select a from WorkData a where a.work.category.type = ?1 group by a.work.id")
    Page<WorkData> pageByType(Integer type, Pageable pageable);

    @Query("select a from WorkData a where a.work.category.type = ?1 and a.work.name like ?2 group by a.work.id")
    Page<WorkData> pageByParams(Integer type, String name, Pageable pageable);

    @Query("select a from WorkData a where a.work.id = ?1")
    List<WorkData> findListByWorkId(Long workId);

    @Query("select a from WorkData a where a.work.id = ?1 and a.detailId = ?2")
    List<WorkData> findListByWorkIdAndDetailId(Long workId, Long detailId);

    @Query("select a from WorkData a where a.work.id = ?1 and a.detailId = ?2 and a.createDate >= ?3 and a.createDate <= ?4")
    List<WorkData> findListByWorkIdAndDetailIdAndAllDate(Long workId, Long detailId, String startDate, String endDate);

    @Query("select a from WorkData a where a.work.id = ?1 and a.detailId = ?2 and a.createDate >= ?3 and a.createDate <= ?4")
    Page<WorkData> pageByWorkIdAndDetailIdAndAllDate(Long workId, Long detailId, String startDate, String endDate, Pageable pageable);

    @Query("select a from WorkData a where a.work.id = ?1 group by a.detailId order by a.detailId")
    Page<WorkData> pageByWorkId(Long workId, Pageable pageable);

    @Query("select a from WorkData a where a.work.id = ?1 and a.createDate >= ?2 and a.createDate <= ?3 group by a.detailId")
    Page<WorkData> pageByParams(Long workId, String startDate, String endDate, Pageable pageable);

    @Query("select a from WorkData a where a.work.id = ?1 and a.work.category.type = ?2 and a.createDate >= ?3 and a.createDate <= ?4 group by a.detailId")
    Page<WorkData> pageByParams(Long workId, Integer type, String startDate, String endDate, Pageable pageable);
}
